﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Marbles.Core.Web.Resources;

namespace Marbles.Core.Web.Controls.WebParts
{
    [DefaultProperty("Url")]
    [ToolboxData("<{0}:FlowPlayer runat=server></{0}:FlowPlayer>")]
    public class FlowPlayer : WebPart
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Url
        {
            get
            {
                String s = (String)ViewState["Url"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Url"] = value;
            }
        }

        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("flowplayer-3.1.5.swf")]
        [Localizable(true)]
        public string FlowPlayerApplicationUrl
        {
            get
            {
                String s = (String)ViewState["FlowPlayerUrl"];
                return ((s == null) ? "flowplayer-3.1.5.swf" : s);
            }

            set
            {
                ViewState["FlowPlayerUrl"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.ClientScript.IsClientScriptIncludeRegistered("FlowPlayer"))
            {
                WebResources res = new WebResources();
                Page.ClientScript.RegisterClientScriptInclude("FlowPlayer", res.FlowPlayerResourceUrl);
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            System.Web.UI.HtmlControls.HtmlAnchor anch = new System.Web.UI.HtmlControls.HtmlAnchor();
            LiteralControl lt = new LiteralControl();
            lt.ID = this.ID + "_script";
            anch.ID = this.ID + "_video";
            anch.HRef = this.Url;
            anch.Style.Add("display", "block");
            if (!this.Width.IsEmpty) { anch.Style.Add("width", this.Width.ToString()); }
            if (!this.Height.IsEmpty) { anch.Style.Add("height", this.Height.ToString()); }
            this.Controls.Add(anch);
            lt.Text = string.Format("<script language=\"javascript\">$f(\"{0}\", \"{1}\");</script>", anch.ClientID, this.FlowPlayerApplicationUrl);
            this.Controls.Add(lt);
        }

    }
}
